from django.db import models


# Create your models here.


# 面试记录表
class Interview(models.Model):
    interview_id = models.AutoField(primary_key=True)
    user_id = models.CharField(max_length=36)  # 关联 User.user_id
    position = models.CharField(max_length=50)  # 面试职位
    start_time = models.DateTimeField(auto_now_add=True)
    end_time = models.DateTimeField(null=True, blank=True)
    video_path = models.CharField(max_length=255)  # 视频路径
    audio_path = models.CharField(max_length=255)  # 音频路径

    class Meta:
        db_table = 'interview'
        indexes = [models.Index(fields=['user_id', 'position'])]


# 面试中提问的问题
class GeneratedQuestion(models.Model):
    generated_q_id = models.AutoField(primary_key=True)
    question_id = models.IntegerField(null=True, blank=True)  # 关联题库
    interview_id = models.CharField(max_length=36)  # 关联 Interview.interview_id
    question_content = models.TextField()  # AI生成的问题内容（动态生成）
    question_type = models.CharField(max_length=50)  # 问题类型（如技术、行为、简历相关）
    generation_source = models.TextField()  # 生成依据的 JSON 描述（如简历字段、岗位JD关键词）

    class Meta:
        db_table = 'generated_question'
        indexes = [models.Index(fields=['interview_id'])]


#  单个回答分析
class AnswerAnalysis(models.Model):
    answer_analysis_id = models.AutoField(primary_key=True)
    interview_id = models.CharField(max_length=36)
    answer_id = models.CharField(max_length=36)
    modality_type = models.CharField(max_length=20, null=True)  # 如 'audio', 'video', 'text'
    metrics = models.JSONField(null=True)  # 存储多模态指标

    class Meta:
        db_table = 'answer_analysis'
        indexes = [models.Index(fields=['interview_id', 'answer_id'])]


# 回答
class Answer(models.Model):
    answer_id = models.AutoField(primary_key=True)
    generated_q_id = models.CharField(max_length=36)  # 关联 GeneratedQuestion.generated_q_id
    content = models.TextField()
    video_path = models.CharField(max_length=255, null=True)  # 回答视频
    audio_path = models.CharField(max_length=255, null=True)  # 回答音频
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

    class Meta:
        db_table = 'answer'
        indexes = [models.Index(fields=['generated_q_id'])]


# 多模态分析结果表
class Analysis(models.Model):
    analysis_id = models.AutoField(primary_key=True)
    interview_id = models.CharField(max_length=36)  # 关联 Interview.interview_id
    modality_type = models.CharField(max_length=20)  # 如 'audio', 'video', 'text'
    metrics = models.JSONField()  # 存储多模态指标
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'analysis'
        indexes = [models.Index(fields=['interview_id'])]


# 整场面试的评测报告表
class Report(models.Model):
    report_id = models.AutoField(primary_key=True)
    interview_id = models.CharField(max_length=36)  # 关联 Interview.interview_id
    metrics = models.JSONField(null=True)  # 存储多模态指标
    generated_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'report'
        indexes = [models.Index(fields=['interview_id'])]


# 岗位配置表
class PositionConfig(models.Model):
    position = models.CharField(max_length=50, primary_key=True)  # 岗位名称（如技术岗）
    weights = models.JSONField()  # 评分权重配置（如专业知识40%）
    question_ids = models.JSONField(null=True, blank=True)  # 关联问题ID列表

    class Meta:
        db_table = 'position_config'


# 题库
class Question(models.Model):
    """
    面试题库表，不使用外键，通过 choices 定义分类
    """
    ROLE_CHOICES = [
        ('python', 'Python 工程师'),
        ('java', 'Java 工程师'),
        ('frontend', '前端'),
        ('devops', '运维'),
        ('test', '测试'),
        ('product', '产品'),
    ]
    DIFFICULTY_CHOICES = [
        ('low', '低'),
        ('medium', '中'),
        ('high', '高'),
    ]
    TYPE_CHOICES = [
        ('theory', '理论概念'),
        ('application', '应用'),
    ]

    question_id = models.AutoField(primary_key=True)
    content = models.TextField(verbose_name='题目内容')
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, verbose_name='岗位')
    difficulty = models.CharField(max_length=10, choices=DIFFICULTY_CHOICES, verbose_name='难度')
    question_type = models.CharField(max_length=20, choices=TYPE_CHOICES, verbose_name='题目类型')
    suggested_answer_time = models.PositiveIntegerField(verbose_name='建议回答时间（分钟）', help_text='建议回答时长')
    key_points = models.TextField(verbose_name='关键回答要点', help_text='回答该题目时的关键要点，可多行')
    reference_answer = models.TextField(verbose_name='参考答案', help_text='题目的标准或示例答案，可多行')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'question'
        ordering = ['-created_at']
